<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_core_Cookie
{
	private static $instance;

    private $name;
    private $expiration;
    private $path = "/";
    private $domaine = HTTP_HOST;
    private $secure = false;
    private $httponly = false;

	/**
	 * @return f_core_Cookie
	 */
	private function __construct()
	{
		return $this;
	}

	/**
	 * Get singleton instance
	 *
	 * @return f_core_Cookie
	 */
	public static function getInstance()
	{
		if(self::$instance === null)
		{
			self::$instance = new self();
		}
		return self::$instance;
    }

    /**
     *  Set the parameters to configure the cookie
     * @param string $name
     * @param int $expiration
     * @param string $path
     * @param string $domaine
     * @param boolean $secure
     * @param boolean $httponly
     * @return object f_core_cookie
     */
    public function setParameters($name, $expiration, $path = "/", $domaine = HTTP_HOST, $secure = false, $httponly = false)
    {
        $this->setName($name);
        $this->setExpiration($expiration);
        
        if(!is_null($path))
        {
            $this->setPath($path);
        }

        if(!is_null($domaine))
        {
            $this->setDomaine($domaine);
        }

        if(!is_null($secure))
        {
            $this->setSecure($secure);
        }

        if(!is_null($httponly))
        {
            $this->setHttpOnly($httponly);
        }

        return $this;
    }

    /**
     *  Set the cookie's name
     * @param string $name
     * @return object f_core_cookie
     */
    public function setName($name)
    {
        if(is_string($name) && !empty($name))
        {
            $this->name = $name;
        }

        return $this;
    }

    /**
     *  Set the time wich must left before the cookie exprirate.
     * @param int $expiration
     * @return object f_core_cookie
     */
    public function setExpiration($expiration)
    {
        if(is_int($expiration))
        {
            $this->expiration = $expiration;
        }

        return $this;
    }

    /**
     *  Set the path wich the cookie will be valid
     * @param int $path
     * @return object f_core_cookie
     */
    public function setPath($path)
    {
        if(is_string($path))
        {
            $this->path = $path;
        }

        return $this;
    }

    /**
     *  Set the domaine wich the cookie will be valid
     * @param string $domaine
     * @return object f_core_cookie
     */
    public function setDomaine($domaine)
    {
        if(is_string($domaine))
        {
            $this->domaine = $domaine;
        }

        return $this;
    }

    /**
     *  Set if the secure mod
     * @param string $domaine
     * @return object f_core_cookie
     */
    public function setSecure($secure)
    {
        if(is_bool($secure))
        {
            $this->secure = $secure;
        }

        return $this;
    }

    /**
     *  Set the httponly mod
     * @param boolean $httponly
     * @return object f_core_cookie
     */
    public function setHttpOnly($httponly)
    {
        if(is_bool($httponly))
        {
            $this->httponly = $httponly;
        }

        return $this;
    }

    /**
     *  Put the value into the cookie
     * @param mixte $value
     */
    public function setCookie($value)
    {
        if(!is_null($this->name))
        {
            $ob = ini_get('output_buffering');
            if(headers_sent() && (bool) $ob === false || strtolower($ob) == 'off')
			{
				return false;
			}
			
            if(is_array($value))
            {
                $table = $this->name;
                foreach($value as $key => $var)
                {
                    setcookie($this->name."[".$key."]", $var, $this->expiration, $this->path, $this->domaine);						
                }
            }
            else
            {
                setcookie($this->name, $value, $this->expiration, $this->path, $this->domaine);
            }

        }

        return true;
    }

    /**
     *  Return the cookie's value
     * @return string
     */
    public function getCookie()
    {
        return isset($_COOKIE[$this->name]) ? $_COOKIE[$this->name] : null;
    }

    /**
     * To delete the cookie
     */
    public function deleteCookie($value = null)
    {
        if(isset($_COOKIE[$this->name]))
        {
            if(is_array($_COOKIE[$this->name]))
            {
                if(!is_null($value))
                {
                    if(is_array($value))
                    {
                        foreach($value as $var)
                        {
                            $this->delete($this->name."[".$var."]");
                        }
                    }
                    else
                    {
                        $this->delete($this->name."[".$value."]");
                    }
                }
                else
                {
                    foreach($_COOKIE[$this->name] as $key => $var)
                    {
                        $this->delete($this->name."[".$key."]");
                    }
                }
            }
            else
            {
                $this->delete($this->name);
            }

            return true;
        }

        return false;
    }

    /**
     *  Delete the cookie
     * @param string $name
     */
    private function delete($name)
    {
        $ob = ini_get('output_buffering');
		if(headers_sent() && (bool) $ob === false || strtolower($ob) == 'off')
		{
			return false;
		}
		else
		{
			setcookie($name, false, time() - 3600, $this->path, $this->domaine);
		}
        return true;
    }
}